package com.thinkTank.oa.controller;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import com.thinkTank.oa.common.Constant;
import com.thinkTank.oa.common.SystemGlobals;
import com.thinkTank.oa.configuration.Log;
import com.thinkTank.oa.configuration.RefPid;
import com.thinkTank.oa.entity.ButtDetails;
import com.thinkTank.oa.entity.Conference;
import com.thinkTank.oa.entity.Customer;
import com.thinkTank.oa.entity.Demand;
import com.thinkTank.oa.entity.Dictiontry;
import com.thinkTank.oa.entity.Operationlog;
import com.thinkTank.oa.entity.Person;
import com.thinkTank.oa.entity.Project;
import com.thinkTank.oa.service.ConferenceService;
import com.thinkTank.oa.service.CustomerService;
import com.thinkTank.oa.service.DemandService;
import com.thinkTank.oa.service.DetailsService;
import com.thinkTank.oa.service.PersonService;
import com.thinkTank.oa.service.ProjectService;
import com.thinkTank.oa.service.UserService;
import com.thinkTank.oa.util.FileUtils;
import com.thinkTank.oa.util.StrUtil;

/**
 * 对接详情controller
 * @author 董力
 * 2018-08-20
 */
@Controller
@RequestMapping("details")
public class ButtDetailsController extends BaseController {

    /* 日志输出 */
    private final Logger logger = LoggerFactory.getLogger(ButtDetailsController.class);
    
    @Resource
    private DetailsService detailsService; 
    @Resource
    private UserService UserService;
    
    @Autowired
    private ProjectService projectService;
    @Autowired
    private DemandService demandService;
    @Autowired
    private PersonService personService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private ConferenceService conferenceService;

    /**
     * 分页查询
     * @author dongli
     * @date 2018年8月20日
     * @param pageNum
     * @param details
     * @param model
     * @return 对接详情列表页
     */
	@RequestMapping(value= "findButtDetailsList",produces="application/json;charset=UTF-8")
	public String findPersonList(Integer pageNum,ButtDetails details,Model model){
		//分页
		if(null==pageNum){
			pageNum=1;
		}
		PageHelper.startPage(pageNum, BaseController.DEFAULT_COUNT);
		//模糊查询person表
		List<ButtDetails> list = detailsService.findMDetailsList(details);
		//将list放进PageInfo
		PageInfo<ButtDetails> pageInfo = new PageInfo<ButtDetails>(list);
		
		model.addAttribute(pageInfo);	
		
		return "buttDetails";
	}
	
	/**
	 * 进入添加页面
	 * @author dongli
	 * @date 2018年8月20日
	 * @param model
	 * @return
	 */
	@RequestMapping(value= "addView",produces="application/json;charset=UTF-8")
	public String addView(Model model){
		//对接状态下拉列表
		List<Dictiontry> buttStatus = getButtStatus();
		
		model.addAttribute("buttStatus", buttStatus);
		
		return "buttDetailsAdd";
	}
	
	/**
	 * 添加方法
	 * @author dongli
	 * @date 2018年8月20日
	 * @param buttDetails
	 * @param bindingResult
	 * @param buttaNnex
	 * @param request
	 * @return
	 */
	@RequestMapping(value= "addDetails",method = RequestMethod.POST,produces="application/json;charset=UTF-8")
	public String addPerson(@Valid ButtDetails buttDetails,BindingResult bindingResult,@RequestParam("buttaNnex1") MultipartFile buttaNnex1,
			HttpServletRequest request,Model model){
		/**
		 * 验证
		 */
		/*if (bindingResult.hasErrors()) {
			model.addAttribute("buttDetails", buttDetails);
			return addView(model);
		}*/
		
		//pid
		buttDetails.setPid(StrUtil.getUUID32());
		//上传附件
		if (!buttaNnex1.isEmpty()) {
			// 文件名字
			String fileName = buttaNnex1.getOriginalFilename();
			// 文件路径
			String filePath = request.getSession().getServletContext().getRealPath("fileUpload/");
			// 将文件路径保存至数据库中
			buttDetails.setButtaNnex(filePath + fileName);
			try {
				FileUtils.uploadFile(buttaNnex1.getBytes(), filePath, fileName);
			} catch (Exception e) {
				logger.error("上传文件失败：" + e.getMessage());
			}
		}
		
		detailsService.addDetails(buttDetails);
		//通过重定向返回专家页面
		return "redirect:/details/findButtDetailsList";
	}
	
	/**
	 * 进入修改页面
	 * @author dongli
	 * @date 2018年8月20日
	 * @param model
	 * @return
	 */
	@RequestMapping(value= "updateView/{pid}",produces="application/json;charset=UTF-8")
	public String updateView(@PathVariable("pid") String pid,Model model){
		//对接状态下拉列表
		List<Dictiontry> buttStatus = getButtStatus();
		//通过pid查询对接详情
		ButtDetails details = detailsService.findDetailsByPid(pid);
		
		model.addAttribute("details", details);
		model.addAttribute("buttStatus", buttStatus);
		
		return "buttDetailsUpdate";
	}
	
	/**
	 * 修改
	 * @author dongli
	 * @date 2018年8月21日
	 * @param buttDetails
	 * @param bindingResult
	 * @param buttaNnex
	 * @param request
	 * @return
	 */
	@RequestMapping(value= "UpdateDetails",produces="application/json;charset=UTF-8",method = RequestMethod.POST)
	public String UpdateDetails(ButtDetails buttDetails,BindingResult bindingResult,
			@RequestParam("buttaNnex1") MultipartFile buttaNnex1,HttpServletRequest request){
		//重新选择附件并上传
		if (!buttaNnex1.isEmpty()) {
			// 文件名字
			String fileName = buttaNnex1.getOriginalFilename();
			// 文件路径
			String filePath = request.getSession().getServletContext().getRealPath("fileUpload/");
			// 将文件路径保存至数据库中
			buttDetails.setButtaNnex(filePath + fileName);
			try {
				FileUtils.uploadFile(buttaNnex1.getBytes(), filePath, fileName);
			} catch (Exception e) {
				logger.error("上传文件失败：" + e.getMessage());
			}
		}
		
		//调用修改方法
		detailsService.updateDetails(buttDetails);;
		
		//通过重定向返回专家页面
		return "redirect:/details/findButtDetailsList";
	}
	
	/***
	 * 根据对接状态查询当前用户 的对接详情
	 * @date 2018-08-23
	 * @return
	 */
	@RequestMapping(value= "showButtDetailsByStatus")
	public String showButtDetailsByStatus(Integer pageNo,Integer status,HttpServletRequest request){
		try{
			 if(null==pageNo){
					pageNo=1;
				}
			 List<Dictiontry> buttStatus=SystemGlobals.getInstance().getDictiontryMap(Constant.DICTIONTRY_BUTTSTATUS);
			 request.setAttribute("buttStatus", buttStatus);
			 PageHelper.startPage(pageNo, DEFAULT_COUNT);
			 String userId =this.getUserId(request);
			 List<ButtDetails> buttDetails = detailsService.findButtDetailsByStatus(userId, status);
			 PageInfo<ButtDetails> pageInfo = new PageInfo<ButtDetails>(buttDetails);
			 pageInfo.getList().forEach(bd -> bd.setButtStatusWorth(getDictiontryWorth(bd.getButtStatus().toString(), buttStatus)));
			 request.setAttribute("pageInfo", pageInfo);
			 request.setAttribute("status", status);
			
		}catch(Exception e){
			logger.error( e.getMessage());
		}
		
		return "showstatusbuttdetails";
	}
	
	/***
	 * @date 2018-08-23
	 * @author dq
	 * @param  detailPid 对接详情的PId
	 * 显示添加对接详情页面
	 * @return
	 */
	@RequestMapping(value= "showAddButtDetails")
	public String showAddButtDetails(String detailPid,Model model){
		List<Dictiontry> buttStatus=SystemGlobals.getInstance().getDictiontryMap(Constant.DICTIONTRY_BUTTSTATUS);
		ButtDetails buttDeteils = new ButtDetails();
		model.addAttribute("buttDeteils", buttDeteils);
		model.addAttribute("detailPid", detailPid);
		model.addAttribute("buttStatus", buttStatus);
		
		ButtDetails de =detailsService.findDetailsByPid(detailPid);
		model.addAttribute("details",de.getButtDetails());
		model.addAttribute("buttDe",de);
		if(null!= de){
			if(de.getProjectNumber()!=null&&!"".equals(de.getProjectNumber())){
				Project project = projectService.getById(de.getProjectNumber());
				model.addAttribute("project", project);
				
			}
			if(de.getDemandNumber()!=null&&!"".equals(de.getDemandNumber())){
							
				Demand demand =demandService.getById(de.getDemandNumber());
				model.addAttribute("demand", demand);
			}
			if(de.getExpertNumber()!=null&&!"".equals(de.getExpertNumber())){
				Person person =personService.getById(de.getExpertNumber());
				model.addAttribute("person", person);
				
			}
			if(de.getCustomerNumber()!=null&&!"".equals(de.getCustomerNumber())){
				Customer customer =customerService.getById(de.getCustomerNumber());
				model.addAttribute("customer", customer);
				
			}
			if(de.getConferenceNumber()!=null&&!"".equals(de.getConferenceNumber())){
				Conference conference = conferenceService.getById(de.getConferenceNumber());
				model.addAttribute("conference", conference);
			}
			
		}
		return "showaddbuttdetails";
	}
    
    
	/***
	 * @date 2018-08-23
	 * @author dq
	 * 添加对接详情页面
	 * @return
	 */
	@RequestMapping(value= "addButtDetails" , method = RequestMethod.POST)
	public String addButtDetails(@Valid ButtDetails buttDetails, BindingResult bindingResult, String detailPid,
			MultipartFile buttDetailsFiles, Model model, HttpServletRequest request){
		Integer statsuid = 1;
			try{
				ButtDetails oldButt = detailsService.findDetailsByPid(detailPid);
				
				buttDetails.setFuture(1);
				buttDetails.setAddDateTime(StrUtil.getCurrentDateTime_two());
				buttDetails.setButtPersonId(this.getUserId(request));
				buttDetails.setButtName(this.getUserInfoBySession(request).getFullName());
				if (null!=buttDetailsFiles && !buttDetailsFiles.isEmpty()) {
					// 文件名字
					String fileName = buttDetailsFiles.getOriginalFilename();
					String oldFileName = FileUtils.generateFileName("buttdetails", fileName);
					
					buttDetails.setButtaNnex(oldFileName);
					try {
						FileUtils.uploadFile(buttDetailsFiles.getBytes(), Constant.DEF_BUT_DETAILS_PATH, oldFileName);
					} catch (Exception e) {
						logger.error("上传文件失败：" + e.getMessage());
					}
				}
				detailsService.addButtDetails(oldButt,buttDetails);
				statsuid= oldButt.getButtStatus();
			}catch(Exception e){
				logger.error( e.getMessage());
			}
		return "redirect:/details/showButtDetailsByStatus?status="+statsuid;
	}
	
	/***
	 * 查看历史对接记录
	 * @param buttdetails 对接记录Id
	 * @param request
	 * @return
	 */
	@RequestMapping("/showHistoryButtDetails")
	public  String showHistoryButtDetails(Integer pageNo,String buttdetails ,HttpServletRequest request){
		try{
			List<Dictiontry> buttStatus=SystemGlobals.getInstance().getDictiontryMap(Constant.DICTIONTRY_BUTTSTATUS);
			request.setAttribute("buttStatus", buttStatus);
			ButtDetails buttd= detailsService.findDetailsByPid(buttdetails);
			if(null==pageNo){
				pageNo=1;
			}
			 PageHelper.startPage(pageNo, DEFAULT_COUNT);
			 buttd.setFuture(null);
			 List <ButtDetails> listbutt = detailsService.findHistoryButtDetails(buttd);
			 PageInfo<ButtDetails> pageInfo = new PageInfo<ButtDetails>(listbutt);
			 pageInfo.getList().forEach(bd -> bd.setButtStatusWorth(getDictiontryWorth(bd.getButtStatus().toString(), buttStatus)));
			 request.setAttribute("pageInfo", pageInfo);
			 request.setAttribute("buttdetails", buttdetails);
		}catch(Exception e){
			logger.error( e.getMessage());
		}
		
		return "showhistroty";
	}
	/***
	 * 下载对接详情文件
	 * @date  2018-08-23
	 * @author dq
	 * @param request
	 * @param response
	 * @param fileName
	 * @return
	 */
	@RequestMapping("/download")
	public String downloadFile(HttpServletRequest request, HttpServletResponse response,String fileName) {
	 
	    if (fileName != null) {
	        //设置文件路径
	        String realPath = Constant.DEF_BUT_DETAILS_PATH;
	        File file = new File(realPath , fileName);
	        if (file.exists()) {
	            response.setContentType("application/force-download");// 设置强制下载不打开
	            response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
	            byte[] buffer = new byte[1024];
	            FileInputStream fis = null;
	            BufferedInputStream bis = null;
	            try {
	                fis = new FileInputStream(file);
	                bis = new BufferedInputStream(fis);
	                OutputStream os = response.getOutputStream();
	                int i = bis.read(buffer);
	                while (i != -1) {
	                    os.write(buffer, 0, i);
	                    i = bis.read(buffer);
	                }
	                //System.out.println("success");
	            } catch (Exception e) {
	                e.printStackTrace();
	            } finally {
	                if (bis != null) {
	                    try {
	                        bis.close();
	                    } catch (IOException e) {
	                        e.printStackTrace();
	                    }
	                }
	                if (fis != null) {
	                    try {
	                        fis.close();
	                    } catch (IOException e) {
	                        e.printStackTrace();
	                    }
	                }
	            }
	        }
	    }
	    return null;
	}
	
	/**
	 * 需求已经匹配的项目更新对接详情
	 * @author pdz
	 * @date 2018年9月3日
	 * @param detailPid 对接ID
	 * @return
	 */
	@RequestMapping("/showDemandMatchProjectAddButt")
	public String showDemandMatchProjectAddButt(String detailPid, Model model, String customerId) {
		showAddButtDetails(detailPid, model);
	    ButtDetails buttDetails = (ButtDetails) model.asMap().get("buttDe");
	    return showAddButt(detailPid, model, "/demand/showProjectButtDetail", customerId, "/demand/showProjectButtDetail?pid="+buttDetails.getDemandNumber());
	}
	@RequestMapping("/showDemandMatchProjectAddButt1")
	public String showDemandMatchProjectAddButt1(String detailPid, Model model, String customerId) {
		showAddButtDetails(detailPid, model);
		ButtDetails buttDetails = (ButtDetails) model.asMap().get("buttDe");
		return showAddButt(detailPid, model, "/demand/showDetail", customerId, "/demand/showDetail?demandId="+buttDetails.getDemandNumber());
	}
	
	// 需求已经匹配的专家更新对接详情
	@RequestMapping("/showDemandMatchExpertAddButt")
	public String showDemandMatchExpertAddButt(String detailPid, Model model, String customerId) {
		showAddButtDetails(detailPid, model);
		ButtDetails buttDetails = (ButtDetails) model.asMap().get("buttDe");
		return showAddButt(detailPid, model, "/demand/showExpertButtDetail", customerId, "/demand/showExpertButtDetail?pid="+buttDetails.getDemandNumber());
	}
	@RequestMapping("/showDemandMatchExpertAddButt1")
	public String showDemandMatchExpertAddButt1(String detailPid, Model model, String customerId) {
		showAddButtDetails(detailPid, model);
		ButtDetails buttDetails = (ButtDetails) model.asMap().get("buttDe");
		return showAddButt(detailPid, model, "/demand/showDetail", customerId, "/demand/showDetail?demandId="+buttDetails.getDemandNumber());
	}
	
	/**
	 * 项目已经匹配的需求更新对接详情
	 * @author pdz
	 * @date 2018年10月8日
	 * @param detailPid
	 * @param model
	 * @return
	 */
	@RequestMapping("/showProjectMatchDemandAddButt")
	public String showProjectMatchDemandAddButt(String detailPid, Model model) {
		showAddButtDetails(detailPid, model);
		ButtDetails buttDetails = (ButtDetails) model.asMap().get("buttDe");
		return showAddButt(detailPid, model, "/project/showDemandButtDetail", null, "/project/showDemandButtDetail?pid="+buttDetails.getProjectNumber());
	}
	@RequestMapping("/showProjectMatchDemandAddButt1")
	public String showProjectMatchDemandAddButt1(String detailPid, Model model) {
		showAddButtDetails(detailPid, model);
		ButtDetails buttDetails = (ButtDetails) model.asMap().get("buttDe");
		return showAddButt(detailPid, model, "/project/showDetail", null, "/project/showDetail?projectId="+buttDetails.getProjectNumber());
	}
	
	private String showAddButt(String detailPid, Model model, String prevPage, String customerId, String showButtDetailUrl) {
		model.addAttribute("prevPage", prevPage);
		model.addAttribute("customerId", customerId);
		model.addAttribute("showButtDetailUrl", showButtDetailUrl);
		return "showAddButts";
	}
	
	@Log("修改对接详情")
	@RequestMapping("/addButts")
	public String addButts(@RefPid @Valid ButtDetails buttDetails, BindingResult bindingResult, String detailPid,
            MultipartFile buttDetailsFiles, Model model, HttpServletRequest request, String showButtDetailUrl) {
		
	    addButtDetails(buttDetails, bindingResult, detailPid, buttDetailsFiles, model, request);
	    return "redirect:"+showButtDetailUrl;
	}
	
	public void queryHistoryButts(Integer pageNo,String buttdetails ,HttpServletRequest request) {
		ButtDetails buttd= detailsService.findDetailsByPid(buttdetails);
		PageHelper.startPage(pageNo, DEFAULT_COUNT);
		buttd.setFuture(null);
		buttd.setButtPersonId(null);
		buttd.setButtStatus(null);
		List <ButtDetails> listbutt = detailsService.findButtDetails(buttd);
		PageInfo<ButtDetails> pageInfo = new PageInfo<ButtDetails>(listbutt);
		pageInfo.getList().forEach(bd -> bd.setButtStatusWorth(getDicCheckboxWorth(bd.getButtStatus().toString(), Constant.DICTIONTRY_BUTTSTATUS)));
		request.setAttribute("pageInfo", pageInfo);
		request.setAttribute("buttdetails", buttdetails);
	}
	
	/**
	 * 需求已匹配项目的历史详情
	 * @author pdz
	 * @date 2018年9月3日
	 * @param buttdetails 对接ID
	 * @return
	 */
	@RequestMapping("/showDemandMatchProjectHistoryButts")
	public String showDemandMatchProjectHistoryButts(@RequestParam(defaultValue="1") Integer pageNo, String buttdetails, 
			HttpServletRequest request, Model model, String customerId, Demand demand, String prevPage) {
		queryHistoryButts(pageNo, buttdetails, request);
		String showHistoryButts = StringUtil.isEmpty(prevPage) ? "/details/showDemandMatchProjectHistoryButts" : "/details/showDemandMatchProjectHistoryButts1";
		prevPage = StringUtil.isEmpty(prevPage) ? "/demand/showProjectButtDetail" : prevPage;
		model.addAttribute("prevPage", prevPage);
		model.addAttribute("customerId", customerId);
		model.addAttribute("demand", demand);
		model.addAttribute("showHistoryButts", showHistoryButts);
		return "showHistoryButts";
	}
	@RequestMapping("/showDemandMatchProjectHistoryButts1")
	public String showDemandMatchProjectHistoryButts1(@RequestParam(defaultValue="1") Integer pageNo, String buttdetails, 
			HttpServletRequest request, Model model, String customerId, Demand demand) {
		return showDemandMatchProjectHistoryButts(pageNo, buttdetails, request, model, customerId, demand, "/demand/showDetail");
	}
	@RequestMapping("/showDemandMatchExpertHistoryButts")
	public String showDemandMatchExpertHistoryButts(@RequestParam(defaultValue="1") Integer pageNo, String buttdetails, 
			HttpServletRequest request, Model model, String customerId, Demand demand, String prevPage) {
		queryHistoryButts(pageNo, buttdetails, request);
		String showHistoryButts = StringUtil.isEmpty(prevPage) ? "/details/showDemandMatchExpertHistoryButts" : "/details/showDemandMatchExpertHistoryButts1";
		prevPage = StringUtil.isEmpty(prevPage) ? "/demand/showExpertButtDetail" : prevPage;
		model.addAttribute("prevPage", prevPage);
		model.addAttribute("customerId", customerId);
		model.addAttribute("demand", demand);
		model.addAttribute("showHistoryButts", showHistoryButts);
		return "showHistoryButts";
	}
	@RequestMapping("/showDemandMatchExpertHistoryButts1")
	public String showDemandMatchExpertHistoryButts1(@RequestParam(defaultValue="1") Integer pageNo, String buttdetails, 
			HttpServletRequest request, Model model, String customerId, Demand demand) {
		return showDemandMatchExpertHistoryButts(pageNo, buttdetails, request, model, customerId, demand, "/demand/showDetail");
	}
	@RequestMapping("/showProjectMatchDemandHistoryButts")
	public String showProjectMatchDemandHistoryButts(@RequestParam(defaultValue="1") Integer pageNo, String buttdetails, 
			HttpServletRequest request, Model model, Project project, String prevPage) {
		queryHistoryButts(pageNo, buttdetails, request);
		String showHistoryButts = StringUtil.isEmpty(prevPage) ? "/details/showProjectMatchDemandHistoryButts" : "/details/showProjectMatchDemandHistoryButts1";
		prevPage = StringUtil.isEmpty(prevPage) ? "/project/showDemandButtDetail" : prevPage;
		model.addAttribute("prevPage", prevPage);
		model.addAttribute("project", project);
		model.addAttribute("showHistoryButts", showHistoryButts);
		return "showHistoryButts";
	}
	@RequestMapping("/showProjectMatchDemandHistoryButts1")
	public String showProjectMatchDemandHistoryButts1(@RequestParam(defaultValue="1") Integer pageNo, String buttdetails, 
			HttpServletRequest request, Model model, Project project) {
		return showProjectMatchDemandHistoryButts(pageNo, buttdetails, request, model, project, "/project/showDetail");
	}
	
	@RequestMapping("/detailStatistics")
	public String detailStatistics(String beginDate, String endDate, String userId, @RequestParam(defaultValue="1") Integer pageNo, Model model) {
		PageHelper.startPage(pageNo, DEFAULT_COUNT);
		List<ButtDetails> bdList = detailsService.detailStatistics(beginDate, endDate, userId);
		PageInfo<ButtDetails> pageInfo = new PageInfo<>(bdList);
		pageInfo.getList().forEach(bd -> {
			bd.setButtStatusWorth(getDicCheckboxWorth(bd.getButtStatus().toString(), Constant.DICTIONTRY_BUTTSTATUS));
		});
		model.addAttribute("pageInfo", pageInfo);
		model.addAttribute("pageInfo_details", true);
		model.addAttribute("route", "/details");
		return "detailStatistics";
	}
	
	@RequestMapping("/updateDetailStatistics")
    public String updateDetailStatistics(String beginDate, String endDate, String userId, Integer operaIndex, String fullname, @RequestParam(defaultValue="1") Integer pageNo, Model model) {
		List<String> operaList = Arrays.asList("修改项目", "修改需求", "修改客户", "修改专家", "修改对接详情");
    	Operationlog operationlog = new Operationlog(operaList.get(operaIndex), userId, beginDate, endDate);
    	PageHelper.startPage(pageNo, DEFAULT_COUNT);
    	List<Operationlog> operalogList = operationlogService.queryAll(operationlog);
    	PageInfo<Operationlog> pageInfo = new PageInfo<>(operalogList);
    	List<String> refPidList = pageInfo.getList().stream().map(Operationlog::getRefPid).collect(Collectors.toList());
    	List<ButtDetails> bdList = new LinkedList<>();
    	refPidList.stream().map(refpid -> refpid.split(",")).forEach(arr -> {
    		//记录对接日志时空number用x代替
    		IntStream.range(0, arr.length).forEach(i -> arr[i] = arr[i].equals("x") ? null : arr[i]);
    		ButtDetails buttDetails = new ButtDetails();
    		buttDetails.setFuture(1);
    		buttDetails.setProjectNumber(arr[0]);
    		buttDetails.setDemandNumber(arr[1]);
    		buttDetails.setCustomerNumber(arr[2]);
    		buttDetails.setExpertNumber(arr[3]);
    		buttDetails.setConferenceNumber(arr[4]);
    		List<ButtDetails> list = detailsService.findButtDetails(buttDetails);
    		bdList.addAll(list);
    	});
		PageInfo<ButtDetails> pageInfo1 = new PageInfo<>(bdList);
		pageInfo1.getList().forEach(bd -> {
			bd.setButtStatusWorth(getDicCheckboxWorth(bd.getButtStatus().toString(), Constant.DICTIONTRY_BUTTSTATUS));
		});
		pageInfo1.setTotal(pageInfo.getTotal());
		pageInfo1.setPages(pageInfo.getPages());
		pageInfo1.setHasPreviousPage(pageInfo.isHasPreviousPage());
		pageInfo1.setPrePage(pageInfo.getPrePage());
		pageInfo1.setNavigatepageNums(pageInfo.getNavigatepageNums());
		pageInfo1.setHasNextPage(pageInfo.isHasNextPage());
		pageInfo1.setNextPage(pageInfo.getNextPage());
		pageInfo1.setLastPage(pageInfo.getLastPage());
		model.addAttribute("pageInfo", pageInfo1);
		model.addAttribute("pageInfo_details", true);
		model.addAttribute("route", "/details");
		return "detailStatistics";
    }
	
	/**
	 * 统计里面的历史对接详情
	 * @author pdz
	 * @date 2018年11月12日
	 * @param pageNo
	 * @param buttdetails 最新一条对接详情的ID
	 * @return
	 */
	@RequestMapping("/showStaHistory")
	public String showStaHistory(@RequestParam(defaultValue="1") Integer pageNo,String buttdetails ,HttpServletRequest request) {
		queryHistoryButts(pageNo, buttdetails, request);
		return "showhistroty";
	}
	
	@RequestMapping("/buttStatistics")
	public String buttStatistics(String beginDate, String endDate, @RequestParam(defaultValue="1") Integer pageNo, Model model) {
		if (StringUtil.isEmpty(beginDate) && StringUtil.isEmpty(endDate)) {
			LocalDate now = LocalDate.now();
			endDate = now.format(DateTimeFormatter.ofPattern("MM/dd/yyyy"));
			beginDate = now.minusDays(7).format(DateTimeFormatter.ofPattern("MM/dd/yyyy"));
		}
		PageHelper.startPage(pageNo, DEFAULT_COUNT);
		ButtDetails buttDetails = new ButtDetails();
		buttDetails.setFuture(1);
		buttDetails.setBeginDate(beginDate);
		buttDetails.setEndDate(endDate);
		List<ButtDetails> bdList = detailsService.findButtDetails(buttDetails);
		PageInfo<ButtDetails> pageInfo = new PageInfo<>(bdList);
		Map<String, List<String>> buttMap = new HashMap<>(bdList.size());
		pageInfo.getList().stream().forEach(bd -> {
			String bdId = bd.getPid();
			if (!buttMap.containsKey(bdId)) {
				List<String> list = Stream.of(bd.getProjectName(), bd.getDemandName(), bd.getCustomerName(), bd.getExpertName())
						.map(name -> StringUtil.isEmpty(name) ? null : name.substring(name.indexOf(":")+1).trim()).collect(Collectors.toList());
				buttMap.put(bdId, list);
			}
		});
		model.addAttribute("pageInfo", pageInfo);
		model.addAttribute("buttMap", buttMap);
		model.addAttribute("beginDate", beginDate);
		model.addAttribute("endDate", endDate);
		return "buttStatistics";
	}
	
	
}
